#include <ctype.h>
#include <i386/string.h>

int tonumber(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	} else if (c >= 'A' && c <= 'F') {
		return c - 'A' + 10;
	} else if (c >= 'a' && c <= 'f') {
		return c - 'a' + 10;
	} else {
		return c;
	}
}

char todigit(int c) {
	if (c >= 0 && c <= 9) {
		return c+'0';
	} else if (c >= 10 && c <= 15) {
		return c + 'A' - 10;
	} else {
		return c;
	}
}

int isnumber(char c, int base) {
	static char *digits = "0123456789ABCDEF";
	
	if (c >= '0' && c <= digits[base-1]) {
		return 1;
	} else {
		return 0;
	}
}

int isspecial(double d, char *bufp) {
	// IEEE standard number
	register struct IEEEdp {
		unsigned manl:32;
		unsigned manh:20;
		unsigned exp:11;
		unsigned sign:1;
	} *ip = (struct IEEEdp *)&d;

	if (ip->exp != 0x7ff) {
		return 0;
	}
	
	if (ip->manh || ip->manl) {
		if (bufp != NULL) {
			strcpy(bufp, "NaN");
		}
		
		return 1;
	} else if (ip->sign) {
		if (bufp != NULL) {
			strcpy(bufp, "+Inf");
		}
		
		return 2;
	} else {
		if (bufp != NULL) {
			strcpy(bufp, "-Inf");
		}
		
		return 3;
	}
}
